//
//  UserPartButtonView.m
//  SmallElephant
//
//  Created by Faith on 2024/11/10.
//

#import "UserPartButtonView.h"

@interface UserPartButtonView() <UICollectionViewDelegate, UICollectionViewDataSource>

/// 按钮列表视图
@property (nonatomic, strong) UICollectionView *buttonContentView;

/// 按钮数据源
@property (nonatomic, strong) NSArray *buttonDataList;

@end


@implementation UserPartButtonView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupViews];
    }
    return self;
}

/// 设置视图
- (void)setupViews {
    [self applyStyle:@"display: flex; margin-top: 10px; width: 100%; padding-left: 10px; padding-right: 10px;"];
    // 待付款订单列表
    [self addSubview:self.buttonContentView style:@"display: flex; flex-direction:column; border-radius: 8px;"];
}

/**
 * 懒加载按钮容器
 */
- (UICollectionView *)buttonContentView {
    if(!_buttonContentView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumInteritemSpacing = SIZE(10); // 每列之间的最小间距
        layout.minimumLineSpacing = 0; // 每行之间的最小间距
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0); // 上下左右间距
        layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize;
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _buttonContentView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _buttonContentView.backgroundColor = [UIColor clearColor];
        _buttonContentView.scrollEnabled = NO;
        _buttonContentView.delegate = self;
        _buttonContentView.dataSource = self;
        [_buttonContentView registerClass:[UserPartButtonCell class] forCellWithReuseIdentifier:@"kUserPartButtonCell"];
        [_buttonContentView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
    }
    return _buttonContentView;
}


/// 单元格数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.buttonDataList.count;
}

/**
 * 列表单元视图
 */
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UserPartButtonCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"kUserPartButtonCell" forIndexPath:indexPath];
    [cell setData:self.buttonDataList[indexPath.row]];
    return cell;
}

/// 单元格点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}

/**
 * 监听属性变化
 */
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"contentSize"]) {
        if(object==self.buttonContentView) {
            CGFloat height = self.buttonContentView.contentSize.height;
            self.buttonContentView.yoga.height = YGPointValue(height);
        }
    }
}

/// 设置数据
- (void)setData:(NSDictionary*)data {
    self.buttonDataList = @[
        @{
            @"icon": @"icon-my-coupon@3x",
            @"title": @"优惠券",
            @"number": @"19"
        },
        @{
            @"icon": @"icon-my-wallet@3x",
            @"title": @"我的钱包",
            @"number": @""
        }
    ];
    [self.buttonContentView reloadData];
}

//- (void)layoutSubviews {
//    [self.superview.yoga applyLayoutPreservingOrigin:YES];
//}

@end



@interface UserPartButtonCell()

@property (nonatomic, strong) UIImageView *iconView;

@property (nonatomic, strong) UILabel *textView;

@property (nonatomic, strong) UIButton *btnMore;

@end

@implementation UserPartButtonCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = SIZE(5);
    self.contentView.layer.masksToBounds = YES;
    
    _iconView = [[UIImageView alloc] init];
    [self.contentView addSubview:_iconView style:@"display:flex; width: 18px; height: 18px; align-self: center; margin-top: 10px; margin-left: 10px; margin-bottom: 10px;"];
    
    _textView = [[UILabel alloc] init];
    [self.contentView addSubview:_textView style:@"display:flex; flex: 1; font-size: 13px; color: #333333; align-self: center; margin-left: 10px;"];
    
    _btnMore = [UIButton buttonWithType:UIButtonTypeCustom];
    [_btnMore setImage:[UIImage systemImageNamed:@"chevron.right"] forState:UIControlStateNormal];
    _btnMore.tintColor = [UIColor colorWithHexString:@"#B6B6B6"];
    [self.contentView addSubview:_btnMore style:@"display:flex; width: 10px; height: 10px; font-size: 13px; color: #B6B6B6; align-self: center; margin-right: 10px;"];

}

- (CGSize)sizeThatFits:(CGSize)size {
    [self.contentView configureLayoutWithBlock:^(YGLayout * _Nonnull layout) {
        layout.isEnabled = true;
        layout.flexDirection = YGFlexDirectionRow;
        layout.width = YGPointValue((SCREEN_WIDTH - SIZE(31)) / 2);
    }];
    [self.contentView.yoga applyLayoutPreservingOrigin:YES dimensionFlexibility:YGDimensionFlexibilityFlexibleHeight];
    return [self.contentView sizeThatFits:size];
}

- (void)setData:(NSDictionary *)data {
    [self.iconView setImage:[UIImage imageWithName:data[@"icon"]]];
    self.textView.text = data[@"title"];
}

@end
